package com.siyuanren.gcocp.web;

import com.siyuanren.gcocp.common.exception.BaseException;
import com.siyuanren.gcocp.common.web.bean.Result;
import com.siyuanren.gcocp.service.AdminService;
import com.siyuanren.gcocp.util.VaildUtil;
import com.siyuanren.gcocp.vo.AdminIdVo;
import com.siyuanren.gcocp.vo.AdminInsertVo;
import com.siyuanren.gcocp.vo.AdminQueryVo;
import com.siyuanren.gcocp.vo.AdminUpdateVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.Map;
import java.util.UUID;

/**
 * Created by   anxing
 * Project_name gcocp-parent
 * Path         com.siyuanren.gcocp.web
 * Date         2018/5/2
 * Time         9:44
 * Description 后台管理相关代码
 */
@RequestMapping("admin")
@Slf4j
@Controller
public class AdminController {
    @Autowired
    AdminService adminService;

    /**
     * 通用插入方法
     * INSERT INTO wx_course VALUES('0','2018-04-02 10:00:41','1','1','1','1','1','1','1','1','1','1','1','1','1')
     */
    @RequestMapping("insert")
    @ResponseBody
    public Result insert(@RequestParam Map<String,String> map) throws Exception {
        if (map == null) {
            throw BaseException.PARAMETER_ERROR;
        }
        if (!map.containsKey("tableName")) {
            throw  new BaseException("tableName 不能为空");
        }
        String table = map.get("tableName");
        map.remove("tableName");
        AdminInsertVo vo = new AdminInsertVo();
        vo.setTableName(table);
        vo.setValues(map);
        adminService.insert(vo);
        return new Result();
    }

    @RequestMapping("delete")
    @ResponseBody
    public Result delete(AdminIdVo vo) throws Exception {
        VaildUtil.check(vo);
        adminService.delete(vo);
        return new Result();
    }
    @RequestMapping("update")
    @ResponseBody
    public Result update(@RequestParam  Map<String,String> map) throws Exception {
        if (map == null) {
            throw BaseException.PARAMETER_ERROR;
        }
        if (!map.containsKey("tableName") || !map.containsKey("id")) {
            throw  new BaseException("tableName or id 不能为空");
        }

        String table = map.get("tableName");
        Integer id = Integer.valueOf(map.get("id"));
        map.remove("tableName");
        map.remove("id");
        AdminUpdateVo vo = new AdminUpdateVo();
        vo.setTableName(table);
        vo.setId(id);
        vo.setValues(map);
        adminService.update(vo);
        return new Result();
    }
    @RequestMapping("select")
    @ResponseBody
    public Result select(AdminQueryVo vo) throws Exception {
        return new Result(adminService.select(vo));
    }
}
